// @FileName  :books.c
// @Time      :2024/3/7 12:34
// @Author    :YKW
//# include <bits/stdc++.h>
//using namespace std;
# include <stdio.h>
# include <stdlib.h>
# include <math.h>
# include <string.h>
# include <stdlib.h>

typedef struct book {
    char name[50];
    char author[20];
    char publisher[30];
    int publicationDate;
    struct book *next;
} book;

void print(book *head) {
    if (head->publicationDate == -1)return;
    printf("%-50s", head->name);
    printf("%-20s", head->author);
    printf("%-30s", head->publisher);
    printf("%-10d", head->publicationDate);
    puts("");
}

void insert(book *opBook, book *head) {
    book *help = head;
    if (help == NULL) {
        head->next = opBook;
        return;
    }
    while (help->next != NULL && strcmp(help->next->name, opBook->name) < 0)
        help = help->next;
    opBook->next = help->next;
    help->next = opBook;
}

void search(char name[], book *head) {
    book *help = head;
    while (help->next != NULL) {
        help = help->next;
        if (strstr(help->name, name)) {
            print(help);
        }
    }
}

void delete(char name[], book *head) {
    book *help = head;
    while (help->next != NULL) {
        help = help->next;
        if (strstr(help->name, name)) {
            help->publicationDate = -1;
        }
    }
}

void main() {
//    freopen("books.txt", "r", stdin);
    FILE *fp = fopen("books.txt", "r");
    char info[100];
    book *head = (book *) malloc(sizeof(book));
    head->next = NULL;
    while (fgets(info, sizeof(info), fp) != NULL) {
        book *newBook = (book *) malloc(sizeof(book));
        sscanf(info, "%s%s%s%d", newBook->name, newBook->author, newBook->publisher, &newBook->publicationDate);
        newBook->next = NULL;
        insert(newBook, head);
    }
    fclose(fp);
//    freopen("CON", "r", stdin);
    while (1) {
        int op = 1;
        scanf("%d", &op);
        if (op == 0) {
            freopen("ordered.txt", "w", stdout);
            while (head->next != NULL) {
                head = head->next;
                print(head);
            }
            return;
        } else if (op == 1) {
            op = 0;
            book *newBook = (book *) malloc(sizeof(book));
            scanf("%s%s%s%d", newBook->name, newBook->author, newBook->publisher, &newBook->publicationDate);
            newBook->next = NULL;
            insert(newBook, head);
        } else if (op == 2) {
            scanf("%s", info);
            search(info, head);
        } else if (op == 3) {
            scanf("%s", info);
            delete(info, head);
        } else {
            return;
        }
    }
}